#include<stdio.h>
#include<stdint.h>
#include<string.h>
#include <stdio.h>
#include <string.h>
#include "T5L_DGUS.h"

T5L_DGUS_Struct T5L_DGUS_Base_Graph_P2P_Trans(Location_Struct *Location,uint16_t P2P_Size,uint16_t Color)
{
    T5L_DGUS_Struct Stu={T5L_DGUS_Head0,T5L_DGUS_Head1,(P2P_Size+1)*4+11,T5L_DGUS_CMD_Write,T5L_DGUS_Base_Graph_Value_Add0,T5L_DGUS_Base_Graph_Value_Add1};
    T5L_DGUS_Base_Graph_P2P_Data_Struct P2P_Stu={0};

    P2P_Stu.P2P_CMD[0]=T5L_DGUS_Base_Graph_P2P_Data_CMD0;
    P2P_Stu.P2P_CMD[1]=T5L_DGUS_Base_Graph_P2P_Data_CMD1;
    P2P_Stu.P2P_Size[0]=P2P_Size>>8;
    P2P_Stu.P2P_Size[1]=P2P_Size&0xFF;
    
    P2P_Stu.Color[0]=Color>>8;
    P2P_Stu.Color[1]=Color&0xFF;

    P2P_Stu.End[0]=T5L_DGUS_Base_Graph_P2P_Data_End0;
    P2P_Stu.End[1]=T5L_DGUS_Base_Graph_P2P_Data_End1;

    uint8_t i=0;
    for(i=0;i<P2P_Size+1;i++)
    {
        P2P_Stu.Location[i].X[0]=Location[i].X>>8;
        P2P_Stu.Location[i].X[1]=Location[i].X&0xFF;

        P2P_Stu.Location[i].Y[0]=Location[i].Y>>8;
        P2P_Stu.Location[i].Y[1]=Location[i].Y&0xFF;
    }
    
    memcpy(&Stu.Data[0],&P2P_Stu,(P2P_Size+1)*4+6);
    memcpy(&Stu.Data[(P2P_Size+1)*4+6],&P2P_Stu.End[0],2);
    return Stu;
}

uint8_t T5L_DGUS_Send(T5L_DGUS_Struct Stu,uint8_t * buf)
{
    memcpy(&buf[0],&Stu,Stu.Size+3);

    uint8_t i=0;
    for(i=0;i<Stu.Size+3;i++)
    {
        printf("%02X ",buf[i]);
    }    
    return 1;
}

//X: 0:100 10:700       X=i*60+100
//Y: 10:40 0:440        Y=440-40.0f*v
Location_Struct Value_to_Location(uint8_t i,float value,float min,float max)
{
    Location_Struct Stu;

    if(i>10){i=10;}

    Stu.X=i*70+50;

    if (value>max){value=max;}
    if (value<min){value=min;}

    Stu.Y=440.0f-400.0f/(max-min)*(value-min);
    
    return Stu;
}

int main()
{
    uint8_t i=0;
    float value[11]={2.2,1.3,5,7,6,9,10,0,5,4.4,5.6};
    uint8_t buf[250];
    Location_Struct Lo_Stu[12];

    Lo_Stu[0].X=0;
    Lo_Stu[0].Y=440;
    for(i=0;i<11;i++)
    {
        Lo_Stu[i+1]=Value_to_Location(i,value[i],0,10);

        T5L_DGUS_Struct Stu=T5L_DGUS_Base_Graph_P2P_Trans(Lo_Stu,i+1,T5L_DGUS_Base_Graph_P2P_Data_Color);
        T5L_DGUS_Send(Stu,buf);
        printf("\n");
  
    }

    return 0;
}

